/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.serv.system.service.impl;

import com.google.common.base.Optional;
import com.jnzc.ocr.common.utils.BusinessUtils;
import com.jnzc.ocr.common.utils.DateUtils;
import com.jnzc.ocr.common.utils.PasswordUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.serv.system.consts.SysUserConsts;
import com.jnzc.ocr.serv.system.entity.SysUser;
import com.jnzc.ocr.serv.system.mapper.SysUserMapper;
import com.jnzc.ocr.serv.system.service.SysUserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhangcm
 * @version 1.0, 2017/8/27
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    public SysUser find(Long id, Integer type) {
        ValidateUtils.isId(id, "用户ID");
        SysUser filter = new SysUser();
        filter.setId(id);
        filter.setType(type);
        SysUser user = sysUserMapper.selectOne(filter);
        BusinessUtils.notNull(user, "用户信息不存在");
        return user;
    }

    @Override
    public SysUser findByUsername(String username, Long companyId) {
        ValidateUtils.notEmpty(username, "用户名");
        ValidateUtils.isId(companyId, "公司ID");
        SysUser filter = new SysUser();
        filter.setCompanyId(companyId);
        filter.setUsername(username);
        return sysUserMapper.selectOne(filter);
    }

    @Override
    public SysUser findLevelOneByUsername(String username, Integer type) {
        ValidateUtils.notEmpty(username, "用户名");
        ValidateUtils.notNull(type, "用户类型");
        SysUser filter = new SysUser();
        filter.setType(type);
        filter.setLevel(SysUserConsts.LEVEL_ONE);
        filter.setUsername(username);
        return sysUserMapper.selectOne(filter);
    }

    public SysUser insert(SysUser obj) {
        obj.setCreateTime(DateUtils.getNow());
        sysUserMapper.insert(obj);
        return obj;
    }

    public void update(SysUser obj) {
        sysUserMapper.updateById(obj);
    }

    public void delete(Long id) {
        sysUserMapper.deleteById(id);
    }
}
